<template>
	<view class="trading">
		<u-sticky offset-top="0" style="background: #FFFFFF;">
			<u-tabs :list="tabList1" :is-scroll="false" :current="active" @change="onChange">
				<view slot="right" style="padding-left: 4px;margin-right: 30px;display: flex;">
					<u-button type="primary" @tap="openPublish(1)" :text="active1==1?'收赠':'收赠'"></u-button>
					<u-button style="margin-left: 40rpx;" @tap="openPublish(2)" type="primary"
						:text="'我的订单'"></u-button>
				</view>
			</u-tabs>
		</u-sticky>
		<view class="buy_list">
			<view class="buy_item" v-for="(item,index) in buyList_t" :key="index">
				<image :src="$fun.imgUrl(item.avatar)" alt="" />
				<view class="info">
					<view class="t">{{item.mobile}} <text>{{item.createtime}}</text></view>
					<view class="m">单价：{{item.money}}</view>
					<view class="m">数量：{{item.num}}</view>
				</view>
				<view class="b" @click="configBuy(true,item)">{{active1==0?'收赠':'收赠'}}</view>
			</view>
		</view>
		<!-- </mescroll-body> -->
		<u-popup :show="show" @close="closeModel()" mode="center">
			<view class="tab_box" :style="{ width: '300px',padding:'20px' }">
				<view class="prop_title">买人{{tabList[active].name}}</view>
				<u-form :model="activeItem" ref="uForm">
					<u-form-item label="单价">
						<u-input v-model="activeItem.money" />
					</u-form-item>
					<u-form-item label="数量">
						<u-input v-model="activeItem.num" />
					</u-form-item>
					<u-form-item label="总价">
						<u-input disabled v-model="activeItem.price" />
					</u-form-item>
					<u-button style="margin-top: 20rpx;" type="primary" @click="configOrder()">买入</u-button>
				</u-form>
			</view>
		</u-popup>
		<u-popup mode="center" :show="publish" @close="closeModel()">
			<view class="tab_box" :style="{ width: '300px',padding:'20px',borderRadius:'20px' }">
				<view class="prop_title">卖出{{tabList[active].name}}</view>
				<u-form ref="uForm1">
					<u-form-item label="单价">
						<u-input v-model="publishMoney" placeholder="请输入单价" />
					</u-form-item>
					<u-form-item label="数量">
						<u-input v-model="publishNum" placeholder="请输入数量" />
					</u-form-item>
					<u-form-item label="总价">
						<u-input disabled v-model="publishTotal" />
					</u-form-item>
				</u-form>
				<u-button style="margin-top: 20rpx;" type="primary" @click="pay_publish()">发布</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				upOption: {
					auto: false, // 是否自动加载
					page: {
						num: 1, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量
					},
					noMoreSize: 5
				},
				active: 0,
				active1: 0,
				// 购买弹框状态
				show: false,
				// 发布弹框状态
				publish: false,
				// 图片
				imgSrc: "",
				// tab
				tabList: [{
					name: '==',
					id: ''
				}],
				tabList1: [{
					name: '收赠',
					id: 0
				}, {
					name: '转赠',
					id: 1
				}],
				// 购买数据
				buyList_t: [],
				// 当前点击数据
				activeItem: '',
				publishMoney: "",
				publishNum: "",
				publishTotal: 0,
			}
		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title: options.name
			})
		},
		onShow() {
			this.upOption.page.num = 1
			this.init()
		},
		onReachBottom() {
			this.upOption.page.num += 1
			if (this.upOption.page.num != 0) {
				this.buyList(this.upOption.page)
			}
		},
		watch: {
			active(val) {
				this.buyList_t = []
				this.downCallback()
				this.upOption.page.num = 1
				this.buyList(this.upOption.page)
			},
			active1(val) {
				this.buyList_t = []
				this.downCallback()
				this.upOption.page.num = 1
				this.buyList(this.upOption.page)
			},
			publishNum(val) {
				this.publishTotal = this.publishMoney * val
			},
			publishMoney(val) {
				this.publishTotal = this.publishNum * val
			},
		},
		methods: {
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
				setTimeout(() => {
					this.refreshList();
				}, 20);
			},
			// 刷新数据
			downCallback() {
				this.buyList_t = []
				// this.mescroll.resetUpScroll(false);
			},
			//刷新列表
			refreshList() {
				this.mescroll && this.mescroll.resetUpScroll(false);
				this.isLoading = true;
			},

			onChange(e) {
				this.active1 = e.id
				console.log(e.id)
			},
			inputChange(e, type) {
				this[type] = e.detail
			},
			getTab() {
				this.$fun.ajax.post('/otc/index', {}).then(res => {
					if (res.code == 1) {
						this.tabList = res.data
						this.buyList(this.upOption.page)
					}
				});
			},
			//初始化
			init() {
				this.getTab()
			},
			closeModel() {
				this.show = false
				this.publish = false
			},
			// 发布交易
			openPublish(type) {
				if (type == 1) {
					this.publish = true
				} else {
					console.log('我的订单')
					this.$fun.jump('/pageB/otcCenter/otcOrder', 0, 0)
				}
			},
			// 切换弹窗
			toggle(type) {
				this[type] = !this[type];
			},
			// 发布
			pay_publish() {
				if (this.publishMoney == '') {
					this.$fun.msg('请输入单价')
					return
				}
				if (this.publishNum == '') {
					this.$fun.msg('请输入数量')
					return
				}
				let prams = {
					wid: this.tabList[this.active].id,
					point: this.publishMoney,
					num: this.publishNum,
					type: this.active1 == 0 ? "user_id" : "sellid"
				}
				console.log(prams)
				this.$fun.ajax.post('/otc/buyer', {
					...prams
				}).then(res => {
					this.$fun.msg(res.msg);
					if (res.code == 1) {
						this.toggle('publish')
						this.publishMoney = ""
						this.publishNum = ""
						this.buyList(this.upOption.page)
					} else if (res.code == 201) {
						this.$fun.jump('/pageA/authentication/authentication', 0, 1200)
					}
				});
			},
			// 重置数据
			publishClose() {
				this.publishMoney = "";
				this.publishNum = ""
				this.publishTotal = ""
			},
			// 获取列表
			async buyList(e) {
				const data = {
					page: e.num,
					pagenum: e.size,
					id: this.tabList[this.active].id,
					type: this.active1 == 0 ? "user_id" : "sellid"
				};
				const res = await this.$fun.ajax.post('/otc/orderlist', {
					...data
				});
				if (res.code == 1) {
					const curList = res.data;
					if (e.num === 1) {
						this.buyList_t = [];
						//第一页清空数据重载
						if (curList.length > 0) {
							uni.pageScrollTo({
								scrollTop: 0,
								duration: 200
							});
						}
					}
					this.buyList_t = this.buyList_t.concat(curList); //追加新数据
					// this.mescroll.endSuccess(curList.length); //结束加载状态
					// this.upOption.noMoreSize = res.data.last_page;
				}
			},
			// 购买 弹框
			configBuy(state, item) {
				if (this.active1 == 0) {
					this.show = state
					if (item) {
						this.activeItem = item
					}
				} else {
					this.activeItem = item
					this.configOrder()
				}

			},
			// 交易记录
			onClickRight() {
				this.$router.push('/tradingOrdrer')
			},
			// 切换tab
			tabChange(index, str) {
				this[str] = index;
			},
			// 确认订单
			configOrder() {
				if (this.active1 == 0) {
					this.configBuy(false)
				}
				uni.showModal({
					content: '确定提交吗?',
					success: (e) => {
						if (e.confirm) {
							let prams = {
								id: this.activeItem.id,
								type: this.active1 == 0 ? 'user_id' : 'sellid'
							}
							this.$fun.ajax.post('/otc/sell', prams).then(res => {
								this.$fun.msg(res.msg)
								if (res.code == 1) {
									// this.downCallback()
									this.upOption.page.num = 1
									if (this.upOption.page.num != 0) {
										this.buyList(this.upOption.page)
									}
								} else if (res.code == 201) {
									this.$fun.jump('/pageA/authentication/authentication', 0, 1200)
								}
							});
						}
					}
				});
			}
		},
		// 跳转订单
		onNavigationBarButtonTap(e) {
			uni.navigateTo({
				url: `./payOrder`
			})
		}
	}
</script>

<style lang="scss">
	.top_flex {
		width: 100%;
		background: #FFFFFF;
		position: fixed;
		// #ifdef APP-PLUS
		top: 0rpx;
		// #endif
		// #ifdef H5
		top: 88rpx;
		// #endif
		z-index: 100;
	}

	.input_box {
		display: flex;
		justify-content: flex-start;
		flex-direction: column;

		.input_item {
			display: flex;
			margin: 0rpx 32rpx;
			padding: 20rpx 0;
			border-bottom: 1rpx solid rgb(50, 50, 51);

			.lable {
				color: rgb(50, 50, 51);
				font-size: 28rpx;
				margin-right: 40rpx;
			}

			.inputPlace {
				color: 969799;
			}
		}
	}

	.order_box {
		height: calc(100vh - 300rpx);
	}

	.van-nav-bar {
		background-color: #3c9cff !important;
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
	}

	page {
		background: #ffffff !important;
	}

	.trading .van-nav-bar {
		background-color: #ffffff;
	}

	.trading .van-nav-bar__title {
		font-family: FZKATJW--GB1-0;
		font-size: 36px;
		font-weight: bold;
		font-stretch: normal;
		letter-spacing: 0px;
		color: #000000;
	}

	.trading .trading[data-v-087d42bb].van-nav-bar__title {
		color: #000000;
	}

	.img_b {
		position: fixed;
		top: 92px;
		z-index: 100;
		height: 300px;
		overflow: hidden;
	}

	.img {
		width: 100%;
		height: 494px;
	}

	.top_buy {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 20rpx;
	}

	.buy_tab {
		padding: 10rpx 0;
		color: #000000;
		font-size: 30rpx;
		border-bottom: 5rpx solid transparent;
	}

	.a {
		color: #3c9cff;
		border-bottom: 5rpx solid #3c9cff;
	}

	.button {
		text-align: center;
		width: 160rpx;
		height: 60rpx;
		line-height: 60rpx;
		background: #ff0302;
		border-radius: 10rpx;
		color: white;
		font-size: 26rpx;
		margin: 0 !important;
	}

	.buy_list {
		display: flex;
		flex-direction: column;
		// #ifdef APP-PLUS
		margin-top: 50rpx;
		// #endif
		// #ifdef H5
		margin-top: 50rpx;

		// #endif
		.buy_item {
			padding: 30rpx;
			display: flex;
			align-items: center;
			position: relative;
			border-bottom: 2rpx solid #f3f3f3;

			image {
				width: 100rpx;
				height: 100rpx;
				margin-right: 15rpx;
				border-radius: 50%;
			}

			.info {
				.t {
					color: #333333;
					font-size: 28rpx;

					text {
						margin-left: 34rpx;
						font-size: 28rpx;
						color: #bdbdbd;
					}
				}

				.m {
					font-size: 26rpx;
					color: #666666;
					margin-top: 10rpx;
				}
			}

			.b {
				position: absolute;
				right: 30rpx;
				top: 0rpx;
				width: 130rpx;
				height: 60rpx;
				background: #3c9cff;
				border-radius: 30rpx;
				color: #ffffff;
				font-size: 26rpx;
				text-align: center;
				line-height: 60rpx;
			}
		}
	}

	.prop_title {
		margin: 10rpx;
		font-size: 32rpx;
		color: #333333;
		text-align: center;
		font-weight: bold;
	}

	.van-button--info {
		background: #3c9cff !important;
	}

	.van-field__body {
		.van-field__control {
			color: #3c9cff !important;
		}
	}

	.van-field__control {
		color: #3c9cff !important;
	}

	.b {
		margin: 40px 30px;

		.van-button--info {
			background: #3c9cff !important;
		}
	}
</style>